package cn.com.dhcc.turbo.dao.registry;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.AbstractCrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.tag.CommonStaticCode.YESNO;
import cn.com.dhcc.app.pub.core.consts.TlqConfig;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.entity.registry.NodePipeline;
import cn.com.dhcc.turbo.entity.registry.wapper.NodePipelineRowWapper;

/**
 * 
 * 传输通道dao
 * 
 * @日期：2014-5-20 下午2:11:15
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Repository
public class NodePipelineDao extends AbstractCrudDao<NodePipeline> {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.SAI);

	public PagerData<NodePipeline> list(Pager pager, WhereCondition condition) throws DaoException{
		String msg = "";
		String sql = "select t1.*,t2.node_name,t2.islocal  from " + SI_TABLE("turbo_node_pipeline") +" t1"
				+" left join " +SI_TABLE("turbo_node_info t2 ")  + " on t1.node_id = t2.id ";
		try {
			return CM.getDao().getPagerList("获取传输通道信息列表", sql,
					new NodePipelineRowWapper(), pager, condition, new Object[] {});
		} catch (DaoException e) {
			msg = "获取传输通道信息列表失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 获取节点已配置传输通道列表
	 * @param nodeId
	 * @return
	 * @throws DaoException
	 */
	public List<NodePipeline> getNodePipelineByNodeId(String nodeId) throws DaoException{
		String msg = "";
		String sql = "select * from " + SI_TABLE("turbo_node_pipeline") +" where node_id=?";
		try {
			return CM.getDao().list("获取节点已配置传输通道列表", sql,
					new NodePipelineRowWapper(),  new Object[] {nodeId});
		} catch (DaoException e) {
			msg = "获取节点已配置传输通道列表失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	
	
	/**
	 * 根据节点编码获取对应TLQ的节点信息
	 * @param nodeId
	 * @return
	 * @throws DaoException
	 */
	public List<NodePipeline> getLocalTlqPiplines() throws DaoException {
		String msg = "";
		String sql = "select * from " + SI_TABLE("turbo_node_pipeline")
				+ " where  isEmbed=? and pipeType=? and islocal=?";
		try {
			return CM.getDao().list("获取当前节点TLQ管道信息", 
					sql, 
					new NodePipelineRowWapper(),
					new Object[] { 0, NodePipeline.PIPE_TYPE.TONGLINKQ.getCode(),YESNO.YES.getStatus()});
		} catch (DaoException e) {
			msg = "获取当前节点TLQ管道信息失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	
	/**
	 * 根据节点编码获取对应TLQ的节点信息
	 * @param nodeId
	 * @return
	 * @throws DaoException
	 */
	public NodePipeline getLocalTlqPipline() throws DaoException {
		String msg = "";
		String sql = "select * from " + SI_TABLE("turbo_node_pipeline")
				+ " where  isEmbed=? and pipeType=? and islocal=? and paramStr like '%'||?||'%' ";
		try {
			return CM.getDao().get("获取当前节点TLQ管道信息", 
					sql, 
					new NodePipelineRowWapper(),
					new Object[] { 0, NodePipeline.PIPE_TYPE.TONGLINKQ.getCode(),YESNO.YES.getStatus() ,TlqConfig.TLQ_BROKER_IP });
		} catch (DaoException e) {
			msg = "获取当前节点TLQ管道信息失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	/**
	 * 根据节点编码获取对应TLQ的节点信息
	 * @param nodeId
	 * @return
	 * @throws DaoException
	 */
	public NodePipeline getTlqPipline(String nodeId) throws DaoException {
		String msg = "";
		String sql = "select * from " + SI_TABLE("turbo_node_pipeline")
				+ " where NODE_ID=? and isEmbed=? and pipeType=?";
		try {
			return CM.getDao().get("获取当前节点TLQ管道信息", 
					sql, 
					new NodePipelineRowWapper(),
					new Object[] {nodeId, 0, NodePipeline.PIPE_TYPE.TONGLINKQ.getCode()});
		} catch (DaoException e) {
			msg = "获取当前节点TLQ管道信息失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	public List<NodePipeline> getLocalNodePipeline() throws DaoException{
		String sql = "select * from " + SI_TABLE("turbo_node_pipeline") +" where ISLOCAL="+YESNO.YES.getStatus();
		try {
			return CM.getDao().list("获取节点已配置传输通道列表", sql,
					new NodePipelineRowWapper(),  new Object[] {});
		} catch (DaoException e) {
			throw new DaoException(e);
		}
	}
	public NodePipeline getLocalNodePipelineById(String id) throws DaoException{
		String sql = "select * from " + SI_TABLE("turbo_node_pipeline") +" where id=? and ISLOCAL="+YESNO.YES.getStatus();
		try {
			return CM.getDao().get("根据通道id获取本地通道信息", sql,
					new NodePipelineRowWapper(),  new Object[] {id});
		} catch (DaoException e) {
			throw new DaoException(e);
		}
	}
	
	/**
	 * 
	 * @param nodeId
	 * @param ip
	 * @return
	 */
	public NodePipeline getByNodeIdAndIp(String nodeId,String ip) throws DaoException{
		String sql = "select * from " + SI_TABLE("turbo_node_pipeline") +" where node_id=? and paramStr like '%'||?||'%' and  pipeType=?";
		try {
			return  CM.getDao().get("获取通道信息失败", sql,new NodePipelineRowWapper(),
					  new Object[] {nodeId,ip,NodePipeline.PIPE_TYPE.TONGLINKQ.getCode()});
		} catch (DaoException e) {
			throw new DaoException(e);
		}
	}
	/**
	 * 修改本地节点的节点id，因为系统还没在管理节点注册时，本地的通道信息时没有节点id的
	 * @param owner
	 * @throws DaoException
	 */
	public void updateNodePipeLineOwner(NodeInfo owner) throws DaoException{
		String sel="select * from TURBO_NODE_PIPELINE where  ISLOCAL="+YESNO.YES.getStatus();
		try {
			List<NodePipeline> list=CM.getDao().listModule("", sel, NodePipeline.class, new Object[]{});
				for (NodePipeline nodePipeline : list) {
					System.out.println("---------查询出来的nodePipeline"+"["+nodePipeline+"]");
				}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
		
		String sql = "update " + SI_TABLE("turbo_node_pipeline") +" t  set NODE_ID=? where  ISLOCAL="+YESNO.YES.getStatus();
		try {
			int result= CM.getDao().update("获取节点已配置传输通道列表", sql,
					  new Object[] {owner.getId()});
			System.out.println("---updateNodePipeLineOwner更新结果--"+result+",sql"+"["+sql+"]");
		} catch (DaoException e) {
			throw new DaoException(e);
		}
	}
	/**
	 * 获取所有通道信息
	 * @param owner
	 * @throws DaoException
	 */
	public List<NodePipeline> getAll() throws DaoException{
		String sql = "select * from " + SI_TABLE("turbo_node_pipeline");
		try {
			return CM.getDao().listModule("获取所有通道信息", sql, NodePipeline.class, new Object[] {});
		} catch (DaoException e) {
			throw new DaoException(e);
		}
	}
}
